package threadTest;

import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

// 1 在main线程中启动两个线程
// 2 第一个线程随机循环打印100以内的整数
// 3 直到第二个线程从键盘读取到'Q'
public class Homework01 {
    public static void main(String[] args) {

        T1 t1 = new T1();
        t1.start();
        T2 t2 = new T2();
        t2.setT1(t1);
        t2.start();
    }
}

class T1 extends Thread{

    private boolean isTrue = true;

    @Override
    public void run(){
        Random random =new Random();
        while (isTrue){
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(random.nextInt(100));
        }
    }

    public void setTrue(boolean aTrue) {
        isTrue = aTrue;
    }
}

class T2 extends Thread{

    private T1 t1;


    public void setT1(T1 t1) {
        this.t1 = t1;
    }

    @Override
    public void run() {
        System.out.println("请输入一个值:");
        Scanner scanner = new Scanner(System.in);
        String word = scanner.next();

        if (Objects.equals(word, "Q")){
           t1.setTrue(false);
        }
    }
}